<template>
  <el-card shadow="never">
    <div slot="header" ref="topHeader" class="sysUserSearchHeader">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-row type="flex" justify="end">
          <el-form-item>
            <el-button size="mini" type="primary" icon="el-icon-search" @click="search" :disabled="isWait">刷新</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div>
      <!--table data-->
      <el-table ref="tableCot" v-loading="loading" element-loading-text="" :data="tableData" border style="width: 100%" :max-height="customTableHeight">
        <el-table-column type="index" width="40"/>
        <el-table-column prop="brand" label="商标" min-width="50" />
        <el-table-column prop="source_name" label="来源" min-width="30" />
        <el-table-column prop="search" label="搜索词" min-width="100" />
        <el-table-column label="ip" min-width="80">
          <template slot-scope="scope">
            <div>ip:{{ scope.row.ip }}</div>
            <div>归属:{{ scope.row.address_ip }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="地址" min-width="130">
          <template slot-scope="scope">
            <div v-if="scope.row.path_f != ''">首次:{{ scope.row.path_f }}</div>
            <div>提交:{{ scope.row.path }}</div>
          </template>
        </el-table-column>
        <el-table-column label="时间" min-width="110" >
          <template slot-scope="scope">
            <div>添加:{{ scope.row.created_at }}</div>
            <div v-if="scope.row.type == 0 && scope.row.update_at > 0">{{ listQuery.page_type === 'receive' ? '领取人' : '放弃人' }}:{{ scope.row.update_at }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="u_nick" :label="listQuery.page_type === 'receive' ? '领取人' : '放弃人'" min-width="50" />
        <el-table-column prop="state" label="状态" min-width="80" />
        <el-table-column prop="phone" label="电话" min-width="70" show-overflow-tooltip>
          <template slot-scope="scope">
            <i style="margin-right: 3px;" v-clipboard:copy='getCopyData(scope.row.phone)' v-clipboard:success='clipboardSuccess' title="复制电话" class="el-icon-copy-document"/>
            <span>{{ scope.row.phone }}</span>
            <div>{{ scope.row.address_phone }}</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.receive_btn === 1" style="cursor:pointer;margin-right: 4px;" size="mini" @click="receiveHandle(scope.row.id)">
              <i class="el-icon-star-on"/>领取
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!--分頁-->
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
    </div>
  </el-card>
</template>
<script>
import Pagination from '@/components/Pagination'
import {getList, receiveInfo } from '@/api/receive'
import permission from '@/directive/permission/index.js'
import clipboard from '@/directive/clipboard/index.js'

export default {
  name: 'tenant',
  components: {Pagination},
  directives: {permission, clipboard},
  data() {
    return {
      loading: false,
      addEditVisible: false,
      operationType: 1,
      customTableHeight: 200,
      formInline: {
        user: '',
        region: ''
      },
      total: 0,
      listQuery: {
        page: 1,
        pageSize: 10,
        name: '',
        status: '',
        page_type: this.$route.name == 'Receive' ? 'receive' : 'shared'
      },
      tableData: [],
      addEditData: {},

      isWait: false
    }
  },
  computed: {
    theme() {
      return this.$store.state.settings.theme
    }
  },
  mounted() {
    this.getList()
    let that = this
    window.onresize = () => {
      //37是box的header的padding，70=(60+10)是顶部的高度
      let tempHeaderHeight = this.$refs.topHeader.offsetHeight + 37 + 95
      let tempHeight = document.body.clientHeight - (106 + tempHeaderHeight)
      if (tempHeight < 300) {
        tempHeight = 300
      }
      that.customTableHeight = tempHeight
    }
    setTimeout(function () {
      const resizeEvent = new Event('resize')
      window.dispatchEvent(resizeEvent)
    }, 100)
  },
  destroyed() {
    window.onresize = null
  },
  methods: {
    search() {
      if (!this.isWait) {
        this.isWait = true
        this.getList()
        setTimeout(() => {
          this.isWait = false
        }, 5000)
      }
    },
    getList() {
      this.loading = true
      if (this.$route.name == 'Receive') {
        this.listQuery.page_type = 'receive'
      } else {
        this.listQuery.page_type = 'shared'
      }
      getList(this.listQuery.page, this.listQuery).then(res => {
        this.tableData = res.data.list || []
        this.total = res.data.total || 0
        this.loading = false
        this.listQuery.page = res.data.page || 1
      }).catch(() => this.loading = false)
    },
    getCopyData(content) {
      return content
    },
    clipboardSuccess() {
      this.$message.success('复制成功')
    },
    receiveHandle(id) {
      receiveInfo(id).then(res => {
        this.$message.success('领取成功')
        this.getList()
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
